<script setup>
import { nextTick, reactive, ref } from "vue";
import { useRouter } from "vue-router";
import { Message } from "@arco-design/web-vue";
import userApi from "@/api/userApi";
import roleApi from "@/api/roleApi";
import schoolApi from "@/api/schoolApi";
import constants from "@/utils/constants";
import SearchTable from "@/components/SearchTable.vue";

const router = useRouter();

const searchListTable = ref();

const submitForm = ref();
const searchForm = ref();
const searchModel = reactive({
  createUserId: "",
});
const visible = ref(false);
let uploadVisible = ref(false);
const loading = ref(false);

let submitData = ref({});
const roleList = ref([]);
const schoolList = ref([]);

let pagination = reactive({
  total: 0,
  current: 1,
  pageSize: 15,
  showPageSize: true,
  pageSizeOptions: [15, 25, 30, 50],
  showTotal: true,
  pageChange: (current) => {
    pagination.current = current;
    search();
  },
  pageSizeChange(size) {
    pagination.pageSize = size;
    search();
  },
});

const columns = [
  {
    title: "用户名",
    dataIndex: "account",
    width: 150,
  },
  {
    title: "管理学校",
    slotName: "school",
  },
  {
    title: "创建时间",
    dataIndex: "createTime",
    width: 180,
  },
  {
    title: "操作",
    slotName: "action",
    fixed: "right",
    width: 200,
  },
];
const data = ref([]);

const checkedAll = ref(false);

const showAddDialog = () => {
  visible.value = true;
  nextTick(() => {
    submitForm.value.resetFields();
    checkedAll.value = false;
    submitData.value = {};
  })
};

const editRecord = (record) => {
  visible.value = true;
  nextTick(() => {
    submitForm.value.resetFields();
    submitData.value = Object.assign({}, record);
  });
};

const deleteRecord = (record) => {
  userApi
    .delete({
      id: record.id,
    })
    .then((res) => {
      search();
    });
};

const handleSubmitSuccess = (values, e) => {
  if (values.schoolIdList && values.schoolIdList.length > 0) {
    values.schoolIds = values.schoolIdList.join(",");
  }
  userApi.save(values).then((res) => {
    visible.value = false;
    Message.success("保存成功");
    search();
  });
};

const search = () => {
  searchListTable.value.load();
};

const refresh = () => {
  search();
};

const getNationName = (value) => {
  let nationItem = constants.nationList.find((res) => {
    return res.value == value;
  });
  return !nationItem ? "" : nationItem.label;
};

const queryRoleList = () => {
  roleApi.queryList().then(res => {
    roleList.value = res;
  })
}

const querySchoolList = () => {
  schoolApi.querySchoolList().then(res => {
    schoolList.value = res;
  })
}

const checkAllSchool = (v) => {
  if (v) {
    submitData.value.schoolIdList = [];
    schoolList.value.forEach(item => {
      submitData.value.schoolIdList.push(item.id);
    })
  } else {
    submitData.value.schoolIdList = [];
  }
}

const reset = () => {
  searchForm.value.resetFields();
};

queryRoleList();
querySchoolList();
</script>

<template>
  <div class="main-con">
    <a-card style="margin-bottom: 16px;">
      <a-row>
        <a-col :flex="1">
          <a-form ref="searchForm" :model="searchModel" :label-col-props="{ span: 8 }" :wrapper-col-props="{ span: 16 }"
            label-align="left">
            <a-row :gutter="16">
              <a-col :span="8">
                <a-form-item field="name" label="用户名">
                  <a-input v-model="searchModel.name" placeholder="请输入用户名" />
                </a-form-item>
              </a-col>
              <a-col :span="8">
                <a-form-item field="roleId" label="所属角色">
                  <a-select v-model="searchModel.roleId" placeholder="请选择所属角色" allow-clear="">
                    <a-option v-for="item in roleList" :key="item.id" :value="item.id" :label="item.name"></a-option>
                  </a-select>
                </a-form-item>
              </a-col>
            </a-row>
          </a-form>
        </a-col>
        <a-divider style="height: 84px" direction="vertical" />
        <a-col :flex="'86px'" style="text-align: right">
          <a-space direction="vertical" :size="18">
            <a-button type="primary" @click="search">
              <template #icon>
                <icon-search />
              </template>
              查询
            </a-button>
            <a-button @click="reset">
              <template #icon>
                <icon-refresh />
              </template>
              重置
            </a-button>
          </a-space>
        </a-col>
      </a-row>
    </a-card>
    <search-table ref="searchListTable" :columns="columns" url="/admin/v1/user/page" :params="searchModel">
      <template #toolbar>
        <a-space>
          <a-button type="primary" @click="showAddDialog">
            <template #icon>
              <icon-plus />
            </template>
            新增账户
          </a-button>
        </a-space>
      </template>
      <template #school="{ record }">
        <a-row :gutter="10">
          <a-col :span="4" v-for="s in record.schoolList" :key="s.id" style="margin-bottom: 5px;">
            <a-tag>{{ s.name }}</a-tag>
          </a-col>
        </a-row>
      </template>
      <template #action="{ record }">
        <a-space>
          <a-button type="text" @click="editRecord(record)">编辑</a-button>
          <a-popconfirm content="确认要删除该数据吗？" @ok="deleteRecord(record)">
            <a-button type="text">删除</a-button>
          </a-popconfirm>
        </a-space>
      </template>
    </search-table>
    <!-- <a-table :columns="columns" row-key="id" :loading="loading" :data="data" @page-change="pagination.pageChange"
      @page-size-change="pagination.pageSizeChange" :pagination="pagination" :bordered="false">
      <template #school="{ record }">
        <a-space><a-tag v-for="s in record.schoolList" :key="s.id">{{ s.name }}</a-tag></a-space>
      </template>
      <template #action="{ record }">
        <a-space>
          <a-button type="text" @click="editRecord(record)">编辑</a-button>
          <a-popconfirm content="确认要删除该数据吗？" @ok="deleteRecord(record)">
            <a-button type="text">删除</a-button>
          </a-popconfirm>
        </a-space>
      </template>
    </a-table> -->
    <a-modal v-model:visible="visible" title="新增账户" :footer="null">
      <a-form ref="submitForm" size="large" :label-col-props="{ span: 6 }" :wrapper-col-props="{ span: 18 }"
        :model="submitData" @submit-success="handleSubmitSuccess">
        <a-form-item field="roleId" label="角色类型" :rules="[{ required: true, message: '请选择角色类型' }]">
          <a-select v-model="submitData.roleId" placeholder="请选择所属角色" allow-clear="">
            <a-option v-for="item in roleList" :key="item.id" :value="item.id" :label="item.name"></a-option>
          </a-select>
        </a-form-item>
        <a-form-item field="account" label="用户名" :rules="[{ required: true, message: '请输入用户名' }]">
          <a-input v-model.trim="submitData.account" placeholder="请输入用户名" />
        </a-form-item>
        <a-form-item field="schoolIdList" label="管理学校">
          <a-space>
            <a-checkbox v-model="checkedAll" @change="checkAllSchool">全选</a-checkbox>
            <a-select v-model="submitData.schoolIdList" :max-tag-count="5" :style="{ width: '290px' }"
              placeholder="请选择管理学校" multiple allow-clear="">
              <a-option v-for="item in schoolList" :key="item.id" :value="item.id" :label="item.name"></a-option>
            </a-select>
          </a-space>
        </a-form-item>
        <a-form-item>
          <div style="color: #999; font-size: 12px">
            注：账号新增成功后，默认登录密码为888888
          </div>
        </a-form-item>
        <a-form-item>
          <a-space>
            <a-button html-type="submit" type="primary">保存</a-button>
            <a-button @click="() => {
              visible = false;
            }
              ">取消</a-button>
          </a-space>
        </a-form-item>
      </a-form>
    </a-modal>
  </div>
</template>

<style scoped></style>
